<div class="layui-card">
    <div class="layui-card-header">
        <h2 class="header-title">问题管理</h2>
        <span class="layui-breadcrumb pull-right">
          <a href="#!console">首页</a>
          <a><cite>问题管理</cite></a>
        </span>
    </div>
    <div class="layui-card-body">
        <div class="layui-form toolbar">
            搜索：
            <select id="question-search-value">
                <option value="">-请选择-</option>
                <option value="1">单选题</option>
                <option value="2">多选题</option>
                <option value="3">主观题</option>
            </select>&emsp;
            <input id="question-edit-search" class="layui-input search-input" type="text" placeholder="输入标题关键字"/>&emsp;
            <button id="question-btn-search" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
            <button id="question-btn-add" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
            <!--<button id="export-question" class="layui-btn icon-btn"><i class="layui-icon">&#xe601;</i>导出</button>-->
        </div>

        <!-- 数据表格 -->
        <table class="layui-table" id="question-table" lay-filter="question-table"></table>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="question-state">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    <a class="layui-btn layui-btn-xs" lay-event="option">选项管理</a>
</script>

<script>
    layui.use(['table', 'config', 'admin', 'layer', 'paperCommon'], function () {
        let table = layui.table;
        let config = layui.config;
        let layer = layui.layer;
        let admin = layui.admin;
        let paperCommon = layui.paperCommon;


        let url = config.base_server + 'api-paper/question/getQuestions';
        let params = {
            access_token: config.getToken().access_token
        };
        let cols = [[
            {type: 'numbers'},
            {field: 'qtitle', sort: true, title: '问题标题'},
            {
                field: 'qutype', sort: true, title: '题型', templet: e => {
                    return paperCommon.questionType(e.qutype)
                }
            },
            {field: 'cretim', sort: true, title: '创建时间'},
            {field: 'updtim', sort: true, title: '修改时间'},
            {templet: '#question-state', width: 200, align: 'center', title: '操作'}
        ]];

        // 渲染表格
        table.render({
            elem: '#question-table',
            url: url,
            where: params,
            page: true,
            cols: cols
        });

        // 工具条点击事件
        table.on('tool(question-table)', function (obj) {
            let data = obj.data;
            let layEvent = obj.event;

            if (layEvent === 'edit') {
                showEditModel(data);
            } else if (layEvent === 'del') {
                layer.confirm('确定删除此条数据吗？', function () {
                    layer.load(2);

                    admin.req('api-paper/question/delete/' + obj.data.quesid, {}, function (data) {
                        layer.closeAll('loading');
                        if (data.code === 0) {
                            layer.msg(data.msg, {icon: 1, time: 500});
                            table.reload('question-table', {});
                        } else {
                            layer.msg(data.msg, {icon: 2, time: 500});
                        }
                    }, 'DELETE');

                });
            } else if (layEvent === 'option') {
                showOptionDialogByData(obj.data);
            }
        });

        // 添加按钮点击事件
        $('#question-btn-add').click(function () {
            showEditModel();
        });

        //显示表单弹窗
        let showEditModel = function (data) {
            admin.putTempData('t_question', data);
            let title = data ? '修改问题' : '添加问题';
            admin.popupCenter({
                title: title,
                path: 'pages/paper/question_form.html',
                finish: function () {
                    table.reload('question-table', {});
                }
            });
        };

        // 搜索按钮点击事件
        $('#question-btn-search').click(function () {
            let key = $('#question-edit-search').val();
            let value = $('#question-search-value').val();
            table.reload('question-table', {where: {searchKey: key, searchValue: value}});
        });

        //导出
        $('#export-question').click(function () {
            let url = config.base_server + 'api-paper/question/exportQuestion?access_token=' + config.getToken().access_token;
            let xhr = new XMLHttpRequest();
            xhr.open('POST', url, true);
            xhr.responseType = "blob";
            xhr.setRequestHeader("client_type", "DESKTOP_WEB");
            xhr.onload = function () {
                if (this.status === 200) {
                    let blob = this.response;
                    window.location.href = URL.createObjectURL(blob);
                }
            };
            xhr.send();
        });

        //选项管理
        let showOptionDialogByData = function (data) {
            admin.putTempData('t_questionOption', data);
            let title = '添加选项';
            admin.popupCenter({
                title: title,
                path: 'pages/paper/questionOption_form.html',
                finish: function () {
                    table.reload('question-table', {});
                }
            });
        };

    });
</script>